# @Time : 2022/6/2 10:55
# @Author : xiashuobad
# @File : constant.py
# @Software: PyCharm
"""
/**
 * _ooOoo_
 * o8888888o
 * 88" . "88
 * (| -_- |)
 *  O\ = /O
 * ___/`---'\____
 * .   ' \\| |// `.
 * / \\||| : |||// \
 * / _||||| -:- |||||- \
 * | | \\\ - /// | |
 * | \_| ''\---/'' | |
 * \ .-\__ `-` ___/-. /
 * ___`. .' /--.--\ `. . __
 * ."" '< `.___\_<|>_/___.' >'"".
 * | | : `- \`.;`\ _ /`;.`/ - ` : | |
 * \ \ `-. \_ __\ /__ _/ .-` / /
 * ======`-.____`-.___\_____/___.-`____.-'======
 * `=---=' bug泛滥 佛已瘫痪
"""
import numpy as np
from enum import Enum, unique
from random import sample
from PIL import Image
from osgeo import gdalconst, ogr

from file_utils import MyPath


class BaseEnum(Enum):
    @classmethod
    def values(cls):
        values = [const.value for const in cls]
        return tuple(values)

    @classmethod
    def names(cls):
        names = [const.name for const in cls]
        return tuple(names)


@unique
class ImageType(BaseEnum):
    """
    常见图片类型枚举常量
    """
    TIF = '.tif'
    TIFF = '.tiff'
    IMG = '.img'
    JPG = '.jpg'
    JPEG = '.jpeg'
    BMP = '.bmp'
    PNG = '.png'

    @classmethod
    def pattern(cls):
        """
        返回模式匹配的模板，适用于文件夹下的扫描过滤
        Returns:

        """
        pattern_ = [f'*{cl.value}' for cl in cls]
        return pattern_

    @classmethod
    def is_image(cls, file):
        """
        判断是否是图片文件格式
        Args:
            file: 文件路径

        Returns:

        """
        return MyPath(file).suffix in cls.values()


@unique
class Color(BaseEnum):
    """
    常见RGB颜色值
    """
    RED = (255, 0, 0)  # 红色
    PINK = (197, 17, 98)  # 粉红色
    PURPLE = (170, 0, 255)  # 紫色
    DEEP_PURPLE = (98, 0, 234)  # 深紫色
    BLUE = (0, 0, 255)  # 蓝色
    LIGHT_BLUE = (0, 145, 234)  # 浅蓝色
    GREEN = (0, 255, 0)  # 绿色
    LIGHT_GREEN = (100, 221, 23)  # 浅绿色
    WHITE = (255, 255, 255)  # 白色
    BLACK = (0, 0, 0)  # 黑色
    DARK_GREEN = (27, 94, 32)  # 深绿色
    YELLOW = (255, 255, 0)  # 黄色
    ORANGE = (255, 109, 0)  # 橙色
    BROWN = (62, 39, 35)  # 棕色
    GREY = (66, 66, 66)  # 灰色
    CYAN = (0, 255, 255)  # 青色

    def show_color(self):
        """
        简单显示下颜色
        Returns:

        """
        img = np.zeros((300, 300, 3), dtype=np.uint8)
        img[:, :] = self.value
        Image.fromarray(img).show()

    @classmethod
    def random_color_map(cls, length, format_type='rasterio'):
        """
        随机生成长度为length的rgb颜色映射表
        Args:
            length: 生成颜色种类的个数
            format_type: 'rasterio' 字典类型：{0:(r,g,b),...)}或者直接返回二维列表类型:[(r,g,b),...]
        Returns:

        """
        rgb_random = sample(cls.values(), length)
        if format_type != 'rasterio':
            return rgb_random
        color_map = {}
        for i, rgb in enumerate(rgb_random):
            color_map[i] = rgb
        return color_map


# dtype类型与gdal数据类型的对应关系
DICT_GDAL_DATA_TYPE = {'uint8': gdalconst.GDT_Byte, 'uint16': gdalconst.GDT_UInt16, 'uint32': gdalconst.GDT_UInt32,
                       'float32': gdalconst.GDT_Float32, 'float64': gdalconst.GDT_Float64}
# 数据类型与shp数据类型的对应关系
DICT_FIELD_TYPE = {int: ogr.OFTInteger, str: ogr.OFTString, float: ogr.OFTReal}
# 项目工程根目录
PROJECT_ROOT_DIR: MyPath = MyPath(__file__).parent
OLD_NORM_CFG = dict(mean=0, std=255)  # 之前的模型的归一化方式
NEW_NORM_CFG = dict(mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375])  # 新模型的归一化方式


